/*	kd:
	This is a bit of a strange thing. The goal is to have the basis for type-
	writer messages. When you say, for example, "add a character every 0.02
	seconds", then 0.02 is way lower than one tic. That means two characters
	should be added in some tics.
	
	And this interface allows you to do just that. */

struct Gc_Ticker {
	/*	Don't get confused here.
	
		Here, a tic is a unit of time, while a tick means "an update".
		Just in case, a tic is 1/35 of a second, and this code will probably
		run once per tic. */
	
	uint    m_ticks_this_tic;
	float   m_ticks_per_tic;
	float   m_time_per_tick;
	float   m_ticks;
	
	void MakeTicker (float time_per_tick = 1) {
		time_per_tick = max(0.00001337, time_per_tick);
		m_ticks       = 0;
		
		m_ticks_per_tic = 1 / (time_per_tick * 35);
	}
	
	void TickUp () {
		m_ticks           += m_ticks_per_tic;
		m_ticks_this_tic   = floor(m_ticks);
		m_ticks           -= m_ticks_this_tic;
	}
}

struct Gc_TypeonMsg play {
	Gc_Ticker       m_ticker;
	Array <String>  m_draw_lines;
	Array <int>		m_offset;
	
	/*	gzd is such a retard that it allows you to make BrokenLines a member of
		a struct, but when it comes to actually initialising it from a saved
		game, it'll vm-abort because it has to initialise this object.
		
		If it is such a huge problem, why can I make it a member to begin with?
		Nothing makes sense here. */
	// BrokenLines     m_lines;
	uint            m_broken_size;
	uint            m_cur_line_size;
	uint            m_line_chars;
	
	uint   m_lines_shown;
	uint   m_chars;
	uint   m_text_len;
	float  m_dest_alpha;
	float  m_line_spacing;
	int    m_break_width;
	int    m_in_color;
	int    m_dest_color;
	float  m_x, m_y;
	bool   m_ticked;
	
	void Init (
		int break_width, float x, float y, Font ffont,
		int in_color = Font.CR_FIRE, int dest_color = Font.CR_SAPPHIRE) {
		
		m_break_width = break_width == 0 ? 0x7ffff666 : break_width;
		m_x				= x;
		m_y				= y;
		m_in_color		= in_color;
		m_dest_color	= dest_color;
		m_line_spacing	= 0;
		m_dest_alpha	= 0;
		m_text_len		= 0;
		m_chars			= 0;
		m_lines_shown	= 0;
		m_line_chars	= 0;
		m_cur_line_size	= 0;
		m_broken_size	= 0;
		m_ticked        = false;
	}
	
	void SetColors (int in_color, int dest_color) {
		m_in_color = in_color;
		m_dest_color = dest_color;
	}
	
	void HorAlign (float f, Font ffont) {
		for(int i = 0; i < m_broken_size; i++) {
			m_offset [i] = f * ffont.StringWidth(m_draw_lines [i]);
		}
	}
	
	void Show (
	String	text,
	double	time_per_char,
	Font	ffont,
	double	line_spacing = (9)) {
		m_chars        = 0;
		m_lines_shown  = 0;
		m_line_chars   = 0;
		m_dest_alpha   = 0;
		m_text_len     = text.Length();
		m_line_spacing = line_spacing;
		// m_line_spacing = ffont.GetHeight();
		m_ticker.MakeTicker(time_per_char);
		
		if(0 < m_text_len) {
			BrokenLines lines = ffont.BreakLines(text, m_break_width);
			m_broken_size = lines.Count();
			
			m_draw_lines.Clear();
			m_draw_lines.Reserve(m_broken_size);
			m_offset.Clear();
			m_offset.Reserve(m_broken_size);
			
			for(uint i = 0; i < m_broken_size; i++) {
				m_offset		[i] = 0;
				m_draw_lines	[i] = lines.StringAt(i);
			}
			
			m_cur_line_size = m_draw_lines [0].Length();
		}
	}
	
	/*	kd:
		I still want to make an improvement. When this is dealt with, don't
		even call this code anymore. */
	void Typeon () {
		if(m_chars < m_text_len && 0 < m_text_len) {
			m_ticker.TickUp();
			m_chars = min(m_chars + m_ticker.m_ticks_this_tic, m_text_len);
			m_line_chars += m_ticker.m_ticks_this_tic;
			m_dest_alpha = 1.0 * m_line_chars / m_cur_line_size;
			m_ticked = true;
			
			if(m_cur_line_size <= m_line_chars) {
				m_lines_shown++;
				m_line_chars -= m_cur_line_size;
				
				if(m_lines_shown < m_broken_size) {
					m_cur_line_size = m_draw_lines [m_lines_shown].Length();
				}
			}
		}
		
		else {
			m_ticked = false;
		}
	}
	
	bool HasTicked () {
		return m_ticked;
	}
	
	bool IsDone () {
		return	m_line_chars	<= m_cur_line_size &&
				m_broken_size	<= m_lines_shown;
	}
	
	ui void Draw (
	RenderEvent	event,
	Font		ffont,
	double		scale = 1,
	double		alpha = 1) const {
		float height = 720 / scale;
		float width  = height * Screen.GetAspectRatio();
		
		float x = m_x * width;
		float y = m_y * height;
		uint i;
		
		for(i = 0; i < m_lines_shown; i++) {
			Screen.DrawText(ffont, m_dest_color,
				x - m_offset [i], y,
				m_draw_lines [i],
				DTA_ALPHA,			alpha,
				DTA_VIRTUALWIDTHF,	width,
				DTA_VIRTUALHEIGHTF,	height,
				DTA_KEEPRATIO,		true);
			/* Screen.DrawText(ffont, m_dest_color,
				x - m_offset [i], y,
				m_draw_lines [i],
				DTA_ALPHA, m_dest_alpha,
				DTA_VIRTUALWIDTHF, width,
				DTA_VIRTUALHEIGHTF, height,
				DTA_KEEPRATIO, true); */
				
			y += m_line_spacing;
		}
		
		if(i <= m_lines_shown && i != m_broken_size) {
			// console.printf("%i", m_line_chars);
			Screen.DrawText(ffont, m_in_color,
				x - m_offset [i], y,
				m_draw_lines [i],
				DTA_TEXTLEN, m_line_chars,
				DTA_VIRTUALWIDTHF, width,
				DTA_VIRTUALHEIGHTF, height,
				DTA_KEEPRATIO, true);
			Screen.DrawText(ffont, m_dest_color, 
				x - m_offset [i], y,
				m_draw_lines [i],
				DTA_TEXTLEN, m_line_chars,
				DTA_ALPHA, m_dest_alpha,
				DTA_VIRTUALWIDTHF, width,
				DTA_VIRTUALHEIGHTF, height,
				DTA_KEEPRATIO, true);
		}
	}
}